package com.zy.core.web.entity;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * Package: core.web
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-9-7 21:50
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class JsonResult {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 状态信息
     */
    private String msg;

    /**
     * 填入自定义参数
     */
    private Map<Object,Object> attr = new HashMap<>();


    @AllArgsConstructor
    public static enum AjaxEnum{
        SUCCESS(0,"调用成功"),
        ERROR(-1,"调用错误"),
        FAILURE(1,"调用失败");

        /**
         * 状态码
         */
        private Integer code;

        /**
         * 状态描述
         */
        private String message;

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    /**
     * 调用成功
     */
    public JsonResult success(){
        success(AjaxEnum.SUCCESS.code, AjaxEnum.SUCCESS.message);
        return this;
    }


    /**
     * 调用成功
     * @param code 状态码
     */
    public JsonResult success(Integer code){
        success(code,AjaxEnum.SUCCESS.message);
        return this;
    }

    /**
     * 调用成功
     * @param msg 描述
     * @return this
     */
    public JsonResult success(String msg){
        success(AjaxEnum.SUCCESS.code,msg);
        return this;
    }

    /**
     * 调用成功
     * @param code 状态码
     * @param msg 状态描述
     */
    public JsonResult success(Integer code, String msg){
        this.msg = msg;
        this.code = code;
        return this;
    }

    /**
     * 调用错误
     * @return this
     */
    public JsonResult error(){
        error(AjaxEnum.ERROR.code,AjaxEnum.ERROR.message);
        return this;
    }

    /**
     * 调用错误
     * @param code 错误码
     * @return this
     */
    public JsonResult error(Integer code){
        error(code,AjaxEnum.ERROR.message);
        return this;
    }

    /**
     * 调用错误
     * @param msg 错误描述
     * @return
     */
    public JsonResult error(String msg){
        error(AjaxEnum.ERROR.code,msg);
        return this;
    }

    /**
     * 调用错误
     * @param code 错误码
     * @param msg 错误描述
     * @return this
     */
    public JsonResult error(Integer code, String msg){
        this.msg = msg;
        this.code = code;
        return this;
    }

    /**
     * 调用失败
     */
    public JsonResult failure(){
        failure(AjaxEnum.FAILURE.code, AjaxEnum.FAILURE.message);
        return this;
    }

    /**
     * 调用失败
     * @param code 失败码
     * @return this
     */
    public JsonResult failure(Integer code){
        failure(code,AjaxEnum.FAILURE.message);
        return this;
    }

    /**
     * 调用失败
     * @param msg 失败描述
     * @return
     */
    public JsonResult failure(String msg){
        failure(AjaxEnum.FAILURE.code,msg);
        return this;
    }


    /**
     * 调用失败
     * @param code 失败码
     * @param msg 失败描述
     * @return this
     */
    public JsonResult failure(Integer code, String msg){
        this.msg = msg;
        this.code = code;
        return this;
    }

    public JsonResult put(Object key,Object value){
        this.attr.put(key,value);
        return this;
    }

    public JsonResult get(Object key){
        this.attr.get(key);
        return this;
    }

    public JsonResult remove(Object key){
        this.attr.remove(key);
        return this;
    }


    /**
     * 输出json字符串
     * @return String
     */
    public String toJsonString(){
        return JSON.toJSONString(this);
    }

}
